package com.android.bookgarden.model;

import android.text.TextUtils;

import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.GetData;
import com.android.bookgarden.network.GetDataImplement;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.network.NethttpCode;
import com.android.bookgarden.network.OkgoCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;

import java.util.Map;

/**
 * Created by zouqh on 2018/4/3.
 */

public class BaseModel implements RealtConstract.Model,OkgoCallback {
    private RealtConstract.OnLoadFirstDataListener listener;
    @Override
    public void loadDataKey(String url, Map<String, String> map, RealtConstract.OnLoadFirstDataListener listener,int urlCode) {
        this.listener = listener;
        GetData getData = new GetDataImplement();
        getData.sendHttp(map, url,this, HttpType.POST,urlCode);
//        List<SearchBean> list=new ArrayList<>();
//        for (int i=0;i<10;i++){
//            list.add(new SearchBean((i+1)+"路"));
//        }
//        this.listener.onSuccess(list);
    }

    @Override
    public void loadDataJson(String url, String json, RealtConstract.OnLoadFirstDataListener listener,int urlCode) {
        this.listener = listener;
        GetData getData = new GetDataImplement();
        if (TextUtils.isEmpty(json)){
            getData.sendHttpGet( url,this, urlCode);
        }else{
            getData.sendHttp(json, url,this, HttpType.POST,urlCode);
        }

    }

    @Override
    public void loadDataKeyHeader(String url, Map<String, String> map, RealtConstract.OnLoadFirstDataListener listener, int urlCode, HttpHeaders headers,HttpType httpType) {
        this.listener = listener;
        GetData getData = new GetDataImplement();
        getData.sendHttpHeader(map, url,this, httpType,urlCode,headers);
    }

    @Override
    public void loadDataKeyGet(String url, Map<String, String> map, RealtConstract.OnLoadFirstDataListener listener, int urlCode, HttpHeaders headers) {
        this.listener = listener;
        GetData getData = new GetDataImplement();
        getData.sendHttpHeader(map, url,this, HttpType.GET,urlCode,headers);
    }

    @Override
    public void onSuccess(String data, int code) {
        this.listener.onSuccess(data,code);
    }

    @Override
    public void onFail(Response<String> response,int code) {
        this.listener.onFailure( NethttpCode.getErrorMsg(response.code()),code,null);
    }
}
